package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager {
	private Connection connection;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;
	private String host;
	private String db;
	private String user;
	private String password;
	private String url;
	private static DBManager uniqueInstance=null;


	public DBManager() {
		// Laad eerst de driver.
		connection = null;
		preparedStatement = null;
		resultSet = null;
		
		//Database gegevens
		//use localhost for deploying on server
		//use ip for deploying localy
//		host = "localhost";
//		host = "145.76.18.59";
		host = "10.1.0.2";
		db = "inti3e";
		user = "root";
		password ="1nt133";
		url = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" + password;
	}
	
	public static synchronized DBManager getInstance() {
		if (uniqueInstance == null) {
			DBManager db = new DBManager();
			db.maakVerbinding();
			uniqueInstance = db;
		}
		return uniqueInstance;
	}
	
	public void resetUrl(String givenUrl){
		url = givenUrl;
		DBManager db = new DBManager();
		db.maakVerbinding();
	}

	public void maakVerbinding() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Laden van de driver is gelukt.");
		} catch (ClassNotFoundException e) {
			System.err.println("Kan de driver niet in de classpath vinden.");
			e.printStackTrace();
			return;
		}

		try {
			// Verkrijg de connectie.
			System.out.println("getconn drivermanager");
			connection = DriverManager.getConnection(url);
			System.out.println(url);

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	
	public Connection getConnection(){
		return connection;
	}
	
	public Statement getPreparedStatement(){
		return preparedStatement;
	}
	
	public ResultSet getResultSet(){
		return resultSet;
	}

	
	 public void close(Connection connection) {
	        if (connection != null) {
	            try {
	                connection.close();
	            } catch (SQLException e) {
	                // Niks aan te doen.
	                System.err.println("Kan de connectie niet sluiten.");
	                e.printStackTrace();
	            }
	        }
	    }

	    public void close(Statement preparedStatement) {
	        if (preparedStatement != null) {
	            try {
	                preparedStatement.close();
	            } catch (SQLException e) {
	                // Niks aan te doen.
	                System.err.println("Kan de statement niet sluiten.");
	                e.printStackTrace();
	            }
	        }
	    }

	    public void close(ResultSet resultSet) {
	        if (resultSet != null) {
	            try {
	                resultSet.close();
	            } catch (SQLException e) {
	                // Niks aan te doen.
	                System.err.println("Kan de resultset niet sluiten.");
	                e.printStackTrace();
	            }
	        }
	    }
}

